package com.hsnn.medstgmini.pro.base.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.form.StdProdDrugInfoForm;
import com.hsnn.medstgmini.pro.base.model.BaseGoods;
import com.hsnn.medstgmini.pro.base.model.BaseShortage;
import com.hsnn.medstgmini.pro.base.service.BaseShortageService;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.model.UserType;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 缺货申报
 * @author 应晓川
 * @date 2015年11月27日10:07:01
 */
@Controller
@RequestMapping("/baseShortage")
public class BaseShortageController {

	private static final Logger log = Logger.getLogger(BaseShortageController.class);
	@Autowired
	private BaseShortageService shortageManager;
	
	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;
	
	/**
	 * @category 查看申报列表
	 * @date 2015年11月28日10:29:21
	 * @return
	 */
	@RequestMapping("/toBaseGoodsList")
	public String toBaseGoodsList(Model model) {
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String url = "404";
		if (user.getType() == 4) { // 医院
			url = "/pro/base/shortage/qualityspecListByHosp";
		} 
//		else if (user.getType() == 3) {// 配送
//			url = "/pro/base/shortage/baseGoodsListByTransComp";
//		}
		return url;
	}
	
	/**
	 * 医院获取申报列表数据
	 * @category param  
	 * @return
	 * @date 2015年11月28日10:29:17
	 */
	@ResponseBody
	@RequestMapping("/getDrugqualityspecListByHosp")
	public Pagination getDrugqualityspecListByHosp(BaseGoods form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		HospInfo hospInfo =(HospInfo)user.getInfo();
		page.getConditions().put("hospCode", hospInfo.getHospCode());
		String drugQualitySpecName = request.getParameter("drugQualitySpecName");
		String drugQualitySpecCode = request.getParameter("drugQualitySpecCode");
		String formName = request.getParameter("formName");
		String specName = request.getParameter("specName");
		
		if(StringUtils.isNoneBlank(drugQualitySpecName)){
			page.getConditions().put("drugQualitySpecName", drugQualitySpecName);
		}
		if(StringUtils.isNoneBlank(drugQualitySpecCode)){
			page.getConditions().put("drugQualitySpecCode", drugQualitySpecCode);
		}
		if(StringUtils.isNoneBlank(formName)){
			page.getConditions().put("formName", formName);
		}
		if(StringUtils.isNoneBlank(specName)){
			page.getConditions().put("specName", specName);
		}
		try {
			page = shortageManager.getDrugqualityspecListByHosp(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 去申报药品页面
	 * @date 2015年11月28日10:29:27
	 * @return
	 */
	@RequestMapping("/toDeclare")
	public String toDeclare(Model model ,StdProdDrugInfoForm sf) {
		model.addAttribute("detail",sf);
		return "/pro/base/shortage/declareShortageInfo";
	}
	
	/**
	 * 医院缺货申报
	 * @category param  
	 * @return
	 * @date 2015年11月28日10:29:17
	 */
	@ResponseBody
	@RequestMapping("/declareShortage")
	public Pagination declareShortage(BaseShortage baseShortage) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		HospInfo hospInfo =(HospInfo)user.getInfo();
		
		baseShortage.setShortageId(UUID.randomUUID().toString());
		Date now =new Date();
		baseShortage.setSubmitDatetime(now);
		baseShortage.setSubmitStatus(Constants.SHORTAGE_SUBMIT);
		baseShortage.setHospCode(hospInfo.getHospCode());
		baseShortage.setHospName(hospInfo.getHospName());
		baseShortage.setHospSpelCode(hospInfo.getHospSpelCode());
		baseShortage.setHospStatus(hospInfo.getStatus());
		if(hospInfo.getHospClassify()==Constants.DEL_RANGE_BASE){//基层医疗机构向卫生局申报
			baseShortage.setBurAuditStatus(Constants.SHORTAGE_NO_AUDIT);
		}else if(hospInfo.getHospClassify()==Constants.DEL_RANGE_COUNTY){//县级以上的向中心申报
			baseShortage.setCetAuditStatus(Constants.SHORTAGE_NO_AUDIT);
		}
		baseShortage.setIsReaded(Constants.NOT_READ);
		baseShortage.setUpdDatetime(now);
		baseShortage.setUpdUser(user.getId());
		shortageManager.addShortage(baseShortage);
		page.setSuccess(true);
		return page;
	}
	
	/**
	 * @category 反馈查询
	 * @date 2015年11月28日16:11:36
	 * @return
	 */
	@RequestMapping("/toFeedbackList")
	public String toFeedbackList(Model model) {
		return "/pro/base/shortage/feedbackList";
	}
	
	/**
	 * 医院获取申报列表数据
	 * @category param  
	 * @return
	 * @date 2015年11月28日10:29:17
	 */
	@ResponseBody
	@RequestMapping("/getFeedbackListByHosp")
	public Pagination getFeedbackListByHosp() {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		HospInfo hospInfo =(HospInfo)user.getInfo();
		page.getConditions().put("hospCode", hospInfo.getHospCode());
		String drugQualitySpecName = request.getParameter("drugQualitySpecName");
		String drugQualitySpecCode = request.getParameter("drugQualitySpecCode");
		String formName = request.getParameter("formName");
		String specName = request.getParameter("specName");
		if(StringUtils.isNoneBlank(drugQualitySpecName)){
			page.getConditions().put("drugQualitySpecName", drugQualitySpecName);
		}
		if(StringUtils.isNoneBlank(drugQualitySpecCode)){
			page.getConditions().put("drugQualitySpecCode", drugQualitySpecCode);
		}
		if(StringUtils.isNoneBlank(formName)){
			page.getConditions().put("formName", formName);
		}
		if(StringUtils.isNoneBlank(specName)){
			page.getConditions().put("specName", specName);
		}
		try {
			page = shortageManager.getFeedbackListByHosp(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 反馈详情查看
	 * @date 2015年11月28日16:11:36
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/toFeedbackDetailInfo")
	public String toFeedbackDetailInfo(String shortageId,String hospClassify) {
		HashMap<String, Object> map = (HashMap<String, Object>) shortageManager.getBaseShortageById(shortageId);
		request.setAttribute("detail", map);
		request.setAttribute("hospClassify",hospClassify);
		return "/pro/base/shortage/feedbackDetailInfo";
	}
	
	/**
	 * @category 反馈详情查看
	 * @date 2015年11月28日16:11:36
	 * @return
	 */
	@RequestMapping("/toAuditList")
	public String toAuditList(String zxAuditTag) {
		request.setAttribute("zxAuditTag", zxAuditTag);
		String title = "2".equals(zxAuditTag)?"基层医院":"县级及县级以上医院";
		request.setAttribute("title", title);
		return "/pro/base/shortage/auditList";
	}
	
	/**
	 * @category 卫生局去审核
	 * @date 2015年11月29日14:57:35
	 * @return
	 */
	@RequestMapping("/toAuditListByWSJ")
	public String toAuditListByWSJ() {
		return "/pro/base/shortage/auditListByWSJ";
	}
	
	
	
	/**
	 * 卫生局获取审核列表
	 * @category param  
	 * @return
	 * @date 2015年11月28日10:29:17
	 */
	@ResponseBody
	@RequestMapping("/getAuditListByWSJ")
	public Pagination getAuditListByWSJ() {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		page.getConditions().put("heaBurId", user.getOrgId());
		String drugQualitySpecName = request.getParameter("drugQualitySpecName");
		String drugQualitySpecCode = request.getParameter("drugQualitySpecCode");
		String formName = request.getParameter("formName");
		String specName = request.getParameter("specName");
		if(StringUtils.isNoneBlank(drugQualitySpecName)){
			page.getConditions().put("drugQualitySpecName", drugQualitySpecName);
		}
		if(StringUtils.isNoneBlank(drugQualitySpecCode)){
			page.getConditions().put("drugQualitySpecCode", drugQualitySpecCode);
		}
		if(StringUtils.isNoneBlank(formName)){
			page.getConditions().put("formName", formName);
		}
		if(StringUtils.isNoneBlank(specName)){
			page.getConditions().put("specName", specName);
		}
		try {
			page = shortageManager.getAuditList(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	
	/**
	 * 医院获取申报列表数据
	 * @category param  
	 * @return
	 * @date 2015年11月28日10:29:17
	 */
	@ResponseBody
	@RequestMapping("/getAuditList")
	public Pagination getAuditList(String zxAuditTag) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		if(UserType.zx.getKey()==user.getType()){//中心
			page.getConditions().put("zxAuditTag", zxAuditTag);//判断审核基层还是县级以上的医疗机构
		}
		String drugQualitySpecName = request.getParameter("drugQualitySpecName");
		String drugQualitySpecCode = request.getParameter("drugQualitySpecCode");
		String formName = request.getParameter("formName");
		String specName = request.getParameter("specName");
		if(StringUtils.isNoneBlank(drugQualitySpecName)){
			page.getConditions().put("drugQualitySpecName", drugQualitySpecName);
		}
		if(StringUtils.isNoneBlank(drugQualitySpecCode)){
			page.getConditions().put("drugQualitySpecCode", drugQualitySpecCode);
		}
		if(StringUtils.isNoneBlank(formName)){
			page.getConditions().put("formName", formName);
		}
		if(StringUtils.isNoneBlank(specName)){
			page.getConditions().put("specName", specName);
		}
		try {
			page = shortageManager.getAuditList(page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 上层机构详情查看
	 * @date 2015年11月29日11:53:12
	 * @return
	 */
	@RequestMapping("/toShortageDetailInfo")
	public String toShortageDetailInfo(String shortageId,String tag,String zxAuditTag) {
		HashMap<String, Object> map = (HashMap<String, Object>) shortageManager.getBaseShortageById(shortageId);
		request.setAttribute("tag", tag);
		request.setAttribute("detail", map);
		request.setAttribute("zxAuditTag", zxAuditTag);
		String title = "2".equals(zxAuditTag)?"基层医院":"县级及县级以上医院";
		request.setAttribute("title", title);
		return "/pro/base/shortage/shortageDetailInfo";
	}
	
	/**
	 * @category  中心审核药品
	 * @date 2015年11月29日11:53:12
	 * @return
	 */
	@RequestMapping("/toAuditShortage")
	public String toAuditShortage(String shortageId) {
		HashMap<String, Object> map = (HashMap<String, Object>) shortageManager.getBaseShortageById(shortageId);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		request.setAttribute("userType", user.getType());
		request.setAttribute("detail", map);
		return "/pro/base/shortage/auditShortage";
	}
	
	/**
	 * @category 卫生局审核药品
	 * @date 2015年11月29日11:53:12
	 * @return
	 */
	@RequestMapping("/toAuditShortageByWSJ")
	public String toAuditShortageByWSJ(String shortageId,String tag) {
		HashMap<String, Object> map = (HashMap<String, Object>) shortageManager.getBaseShortageById(shortageId);
		request.setAttribute("tag", tag);
		request.setAttribute("detail", map);
		return "/pro/base/shortage/auditShortageByWSJ";
	}
	
	/**
	 * 医院缺货申报
	 * @category param  
	 * @return
	 * @date 2015年11月28日10:29:17
	 */
	@ResponseBody
	@RequestMapping("/declareShortageByWSJ")
	public Pagination declareShortageByWSJ(BaseShortage baseShortage) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		baseShortage.setUpdUser(user.getId());
		shortageManager.updateByWSJ(baseShortage);
		page.setSuccess(true);
		return page;
	}
	
	/**
	 * 中心审核
	 * @category param  
	 * @return
	 * @date 2015年11月29日17:21:04
	 */
	@ResponseBody
	@RequestMapping("/declareShortageByZX")
	public Pagination declareShortageByZX(BaseShortage baseShortage) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		baseShortage.setUpdUser(user.getId());
		shortageManager.updateByZX(baseShortage);
		page.setSuccess(true);
		return page;
	}
}
